package com.pocketsweet.ui.uilib.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVInstallation;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVPush;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.FunctionCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;
import com.pocketsweet.C;
import com.pocketsweet.MLApplication;
import com.pocketsweet.MLContext;
import com.pocketsweet.R;
import com.pocketsweet.model.MLArticle;
import com.pocketsweet.model.MLSignRecord;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.model.MLUserLike;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.TopicArticle;
import com.pocketsweet.ui.fragments.NewOnlineFragment;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.SlideView;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Kam
 * 
 */
public class SignRecordAdapter extends BaseListAdapter<MLSignRecord> {
	private List<MLUser> user;
	private int i = 0;
	private ArrayList<String> userLikeID = new ArrayList<String>();
	private Context context;


	public SignRecordAdapter(Context ctx) {
		super(ctx);
		this.context = ctx;
	}

	public SignRecordAdapter(Context ctx, List<MLSignRecord> datas) {
		super(ctx, datas);
		this.context = ctx;
	}

	@Override
	public int getCount() {
		if (datas != null && datas.size() > 0) {
			return datas.size();
		} else {
			return 0;
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_sign_squre, parent, false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	public class ViewHolder implements OnClickListener {
		ImageView imgAvatar;
		TextView tvuserName;
		TextView tvUserAge;
		TextView tvHowLong;
		TextView tvStatus;
		TextView tvTimeDiff;
		MLSignRecord item;
		MLUser mlUser;
		RelativeLayout holder;
		private View parent;
		public LoadingDailog loading;

		public ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatar);
			tvuserName = (TextView) parent.findViewById(R.id.tvuserName);
			tvUserAge = (TextView) parent.findViewById(R.id.tvOnlineUserAge);
			tvHowLong = (TextView) parent.findViewById(R.id.tvHowLong);
			tvTimeDiff = (TextView) parent.findViewById(R.id.tvTimeDiff);
			tvStatus = (TextView) parent.findViewById(R.id.tvStatus);
			holder = (RelativeLayout) parent.findViewById(R.id.holder);
			loading = ToolKits.createLoadingDialog(ctx, "提交中..");
			loading.setText("加载中");

		}

		public void refreshView(MLSignRecord item, int i) {
			this.item = item;
			mlUser=item.getUser();
			String suburl = mlUser.getSubAvatar();
			String avatar = (suburl != null && !suburl.equals("") && !suburl
					.equals("null")) ? mlUser.getSubAvatar() : mlUser
					.getAvatarUrl();
			if (avatar != null && !avatar.equals("")) {
				UserService.displaySquare(avatar, imgAvatar);
				imgAvatar.setTag(avatar);
			} else {
				imgAvatar.setImageResource(R.drawable.default_user_avatar);
			}
			// 设置姓名
			tvuserName.setText(mlUser.getNickname());
			// 设置年龄
			if (!StringUtils.isEmpty(String.valueOf(mlUser.getAgeNum()))) {
				tvUserAge.setText(String.valueOf(mlUser.getAgeNum()));
				tvUserAge.setVisibility(View.VISIBLE);
			} else {
				tvUserAge.setVisibility(View.GONE);
			}
			// 设置性别颜色
			if (mlUser.getGender() == 1) {
				tvUserAge
						.setBackgroundResource(R.drawable.shape_rectangle_bule);
			} else if (mlUser.getGender() == 2) {
				// 设置颜色
				tvUserAge.setBackgroundResource(R.drawable.shape_rectangle_red);

			}
			tvStatus.setText(item.getMood());
			// 设置描述
			tvHowLong.setText("已经晚睡"+String.valueOf(mlUser.getSignCount())+"天...");
			tvTimeDiff.setText(ToolKits.showCommentTime(item.getCreatedAt()));// 几分钟之前

		}


		@Override
		public void onClick(View v) {
			if (v.equals(parent)) {
				// TODO 查看用户资料
			}
		}
	}
}
